/**
 ****************************************************************************************************
 * @file        usart.h
 * @author      LXP
 * @version     V1.0
 * @date        2024-08-08
 * @brief       串口代码，支持printf

 *****************************************************************************************************/

#ifndef _USART_H
#define _USART_H

#include "stm32f4xx_hal.h"
#include "main.h"

/*******************************************************************************************************/


#define USART_RS232                        USART1
#define USART_RS232_IRQn                   USART1_IRQn
#define USART_RS232_IRQHandler             USART1_IRQHandler
#define USART_RS485                        USART3
#define USART_RS485_IRQn                   USART3_IRQn
#define USART_RS485_IRQHandler             USART3_IRQHandler
#define USART_TTL                          USART2
#define USART_TTL_IRQn                     USART2_IRQn
#define USART_TTL_IRQHandler               USART2_IRQHandler

#define UART_RS485_DIR_TX 		HAL_GPIO_WritePin(USART3_DIR_GPIO_Port, USART3_DIR_Pin, SET)
#define UART_RS485_DIR_RX 		HAL_GPIO_WritePin(USART3_DIR_GPIO_Port, USART3_DIR_Pin, RESET)

/*******************************************************************************************************/
//串口是否有忙线，无0，有1
#define RS232_BUSY_EN 0
#define RS485_BUSY_EN 0
#define TTL_BUSY_EN 0
/* UART句柄 */
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;
//三个串口缓冲区长度
#define RS232_REC_LEN   100
#define RS485_REC_LEN   100
#define TTL_REC_LEN     100
/* 接收缓冲,最大USART_REC_LEN个字节.末字节为换行符 */
extern volatile uint8_t g_rs232_rx_buf[RS232_REC_LEN];
extern volatile uint8_t g_rs485_rx_buf[RS485_REC_LEN];
extern volatile uint8_t g_ttl_rx_buf[TTL_REC_LEN];
/* 接收状态标记 */
//extern volatile uint16_t g_rs232_rx_sta;
//extern volatile uint16_t g_rs485_rx_sta;
//extern volatile uint16_t g_ttl_rx_sta;
/* HAL库USART接收Buffer */
#define RXBUFFERSIZE 1
extern volatile uint8_t  g_rs232_rxByte[RXBUFFERSIZE];
extern volatile uint8_t  g_rs485_rxByte[RXBUFFERSIZE];
extern volatile uint8_t  g_ttl_rxByte[RXBUFFERSIZE];



/* 串口初始化函数 */
void usart_init();
//获取缓冲区有效字节数
uint16_t RS232_GetBuffValidByte();
uint16_t RS485_GetBuffValidByte();
uint16_t TTL_GetBuffValidByte();
//获取缓冲区空余字节数
uint16_t RS232_GetBuffEmptyByte();
uint16_t RS485_GetBuffEmptyByte();
uint16_t TTL_GetBuffEmptyByte();
//获取缓冲区1个字节
uint8_t  RS232_GetByte();
uint8_t  RS485_GetByte();
uint8_t  TTL_GetByte();

#endif







